package cn.com.headfree.workflowant.controller;


import javax.servlet.http.HttpServletRequest;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gta.edu.sdk.common.dto.Page;
import cn.com.headfree.base.controller.ExBaseController;
import cn.com.headfree.base.controller.ResultData;
import cn.com.headfree.call.IHumanManageWorkFlowServerCall;
import cn.com.headfree.entity.TBusinessWorkflowBind;
import cn.com.headfree.entity.isc.User;
import cn.com.headfree.vo.workflow.ProcessListVo;
import cn.com.headfree.workflowant.dto.ApproveWorkFlowDto;
import cn.com.headfree.workflowant.service.IWorkFlowAntService;
import net.sf.json.JSONArray;

/**
 * @Title  WorkFlowAntController.java
 * @Package cn.com.headfree.workflowant.controller
 * @Description 流程controller
 * @author yangwang
 * @date  2020年7月29日 下午5:39:17
 */
@CrossOrigin("*")
@Controller
@RequestMapping(value = "/workFlowAntDing", produces = "application/json;charset=UTF-8")
public class WorkFlowAntController extends ExBaseController {
	private static final Logger LOG = LogManager.getLogger(WorkFlowAntController.class);

    @Autowired
    private IWorkFlowAntService workFlowAntService;

    @Autowired
	private IHumanManageWorkFlowServerCall humanManageWorkFlowServerCall;
    /**
	 * 获取所有一级单位
	 * @author yangwang
	 * @return  List<TOrgDept>
	 *  */
    @RequestMapping(value = "/getWorkFlowUnits")
	@ResponseBody
	public JSONArray getWorkFlowUnits(){
    	JSONArray array = null;
		try {
//			List<TOrgDept> list = workFlowAntService.getWorkFlowUnits();
//			array = JSONArray.fromObject(list);
//			LOG.info("获取所有一级单位成功");

			array=humanManageWorkFlowServerCall.getWorkFlowUnits();
		} catch(Exception e) {
			LOG.error("获取所有一级单位异常", e);
			array = new JSONArray();
		}
		return array;
    }

    /**
	 * @description 获取所有业务类型
	 * @author yangwang
	 * @date 20200729 14:48
	 * @return JSONArray
	 */
	@RequestMapping(value = "/getMouldInfoList")
	@ResponseBody
	public JSONArray getMouldInfoList() {
		JSONArray array = null;
		try {
//			List<TMouldInfo> list = workFlowAntService.getMouldInfoList();
//			array = JSONArray.fromObject(list);
//
			array=humanManageWorkFlowServerCall.getMouldInfoList();

			LOG.info("获取所有业务类型成功");
		} catch(Exception e) {
			LOG.error("获取所有业务类型异常", e);
			array = new JSONArray();
		}
		return array;
	}

	/**
	 * @description 获取当前人待办/已办业务类型
	 * @author yangwang
	 * @date 20200729 14:48
	 * @param request
	 * @param status (0-待审核,1-通过,2-不通过,3-驳回，-1-全部)
	 * @return ResultData
	 */
	@RequestMapping(value = "/getMouldsOfUser")
	@ResponseBody
	public ResultData getMouldsOfUser(HttpServletRequest request,ProcessListVo process) {
		ResultData result = new ResultData();
		try {
			LOG.info("=====================调用进度查看获取业务类型1====================");
			User user = this.getLoginUserInfo(request);
			LOG.info("=====================调用进度查看获取业务类型2===================="+user.getName()+"================="+process.getFlowStatus());
			if(user != null){
				process.setDealErpNo(user.getFax());
			}
			result=humanManageWorkFlowServerCall.getMouldsOfUserDing(process);
//			User user = this.getLoginUserInfo(request);
//			if((null != user)&&(StringUtils.isNotBlank(user.getId()))) {
//				process.setDealErpNo(user.getFax());
//				result.setData(workFlowAntService.getMouldsOfUser(process));
//				LOG.info("获取业务类型成功");
//			}else {
//				LOG.error("获取业务类型异常");
//				result.setSuccess(false);
//				result.setErrorMsg("获取业务类型异常");
//			}
		} catch(Exception e) {
			LOG.error("获取业务类型异常", e);
			result.setSuccess(false);
			result.setErrorMsg("获取业务类型异常");
			e.printStackTrace();
		}

		return result;
	}

    /**
	 * @description 根据业务编号获取系统配置的工作流模板
	 * @author yangwang
	 * @date 20200729 14:48
	 * @return JSONArray
	 */
	@RequestMapping(value = "/getWorkFlowTemplates")
	@ResponseBody
	public JSONArray getWorkFlowTemplates() {
		JSONArray array = null;
		try {

			array=humanManageWorkFlowServerCall.getWorkFlowTemplates();

//			ResultData result = workFlowAntService.getWorkFlowTemplates();
//			array = JSONArray.fromObject(result.getData());
			LOG.info("获取系统配置的工作流模板成功");
		} catch(Exception e) {
			LOG.error("获取系统配置的工作流模板出现异常", e);
			array = new JSONArray();
		}
		return array;
	}

	/**
	 * @description 业务类型绑定流程
	 * @author yangwang
	 * @date 20200729 14:48
	 * @param deptId 单位id
	 * @param moduleId 类型id
	 * @param workFlowId 流程id
	 * @return ResultData
	 */
	@RequestMapping(value = "/bindWorkFlowId/{deptId}/{moduleId}/{workFlowId}")
	@ResponseBody
	public ResultData bindWorkFlowId(@PathVariable("deptId") String deptId,@PathVariable("moduleId") String moduleId,@PathVariable("workFlowId") String workFlowId) {
		ResultData resultData = new ResultData();
		try {
			TBusinessWorkflowBind bind = new TBusinessWorkflowBind();
			bind.setOrgId(deptId);
			bind.setBusinessTypeId(moduleId);
			bind.setFlowId(workFlowId);
			workFlowAntService.updateWorkFlowBind(bind);
			LOG.info("业务类型绑定流程成功");
		} catch(Exception e) {
			LOG.error("业务类型绑定流程异常",e);
			resultData.setSuccess(false);
			resultData.setErrorMsg("业务类型绑定流程异常");
		}
		return resultData;
	}

	/**
	 * 获取待办业务流程数据
	 * @author yangwang
	 * @param page 分页参数
	 * @param user 统一权限用户
	 * @return ResultData
	 *  */
	@RequestMapping(value = "/getWaitApproveWorkFlows")
	@ResponseBody
	public ResultData getWaitApproveWorkFlows(HttpServletRequest request,Page<ApproveWorkFlowDto> page) {
		ResultData resultData = new ResultData();
		try {
			User user = this.getLoginUserInfo(request);
			if(user != null){
				ApproveWorkFlowDto approveWorkFlowDto = new ApproveWorkFlowDto();
				resultData=humanManageWorkFlowServerCall.getWaitApproveWorkFlowsDing(page.getPageNo(),page.getPageSize(),user.getId(),user.getBaseOrgId(),approveWorkFlowDto);
			}
//			resultData = workFlowAntService.getWaitApproveWorkFlows(page, this.getLoginUserInfo(request));
			LOG.info("获取待办业务流程数据成功");
		} catch(Exception e) {
			LOG.error("获取待办业务流程数据异常",e);
			resultData.setSuccess(false);
			resultData.setErrorMsg("获取待办业务流程数据异常");
		}
		return resultData;
	}

	/**
	 * 获取已办业务流程数据
	 * @author yangwang
	 * @param page 分页参数
	 * @param user 统一权限用户
	 * @return ResultData
	 *  */
	@RequestMapping(value = "/getDoneApproveWorkFlows")
	@ResponseBody
	public ResultData getDoneApproveWorkFlows(HttpServletRequest request,Page<ApproveWorkFlowDto> page) {
		ResultData resultData = new ResultData();
		try {
			User user = this.getLoginUserInfo(request);
			if(user != null){
				ApproveWorkFlowDto approveWorkFlowDto = new ApproveWorkFlowDto();
				resultData=humanManageWorkFlowServerCall.getDoneApproveWorkFlowsDing(page.getPageNo(),page.getPageSize(),user.getId(),user.getBaseOrgId(),approveWorkFlowDto);
			}
//			resultData = workFlowAntService.getDoneApproveWorkFlows(page, this.getLoginUserInfo(request));
			LOG.info("获取已办业务流程数据成功");
		} catch(Exception e) {
			LOG.error("获取已办业务流程数据异常",e);
			resultData.setSuccess(false);
			resultData.setErrorMsg("获取已办业务流程数据异常");
		}
		return resultData;
	}

	/**
	 * 获取下一审批节点数据(按钮)
	 * @author yangwang
	 * @param approveId 审批id
	 * @return ResultData
	 *  */
	@RequestMapping(value = "/getNextWorkFlowConditions")
	@ResponseBody
	public ResultData getNextWorkFlowConditions(String approveId) {
		ResultData resultData = new ResultData();
		try {

			resultData=humanManageWorkFlowServerCall.getNextWorkFlowConditionsDing(approveId);

//			resultData = workFlowAntService.getNextWorkFlowConditions(approveId);
			LOG.info("获取下一审批节点数据成功");
		} catch(Exception e) {
			LOG.error("获取下一审批节点数据异常", e);
			resultData.setSuccess(false);
			resultData.setErrorMsg("获取下一审批节点数据异常");
		}
		return resultData;
	}

	/**
	 * 审批业务流程
	 * @author yangwang
	 * @param businessTypeId 业务类型id
	 * @param businessId 业务id
	 * @param approveId 审批节点id
	 * @param approveResult 审批状态(1-通过,2-不通过)
	 * @param approveAdvise 审批意见
	 * @param approveCondition 审批条件
	 * @return ResultData
	 *  */
	@RequestMapping(value = "/approveWorkFlow")
	@ResponseBody
	public ResultData approveWorkFlow(HttpServletRequest request,@RequestBody ApproveWorkFlowDto flowDto) {
		ResultData resultData = new ResultData();
		try {
//			if(null != flowDto) {
//				if(StringUtils.isNotBlank(flowDto.getBusinessId())) {
//					if(StringUtils.isNotBlank(flowDto.getApproveId())) {
//						User user = this.getLoginUserInfo(request);
//						if((null != user)&&(StringUtils.isNotBlank(user.getId()))) {
//							flowDto.setCurrentLoginErpNo(user.getFax());
//							flowDto.setIscUserId(user.getId());
//							flowDto.setUserName(user.getName());
//							workFlowAntService.approveWorkFlow(user, flowDto);
//							LOG.info("审批业务流程成功");
//						}else {
//							LOG.error("获取统一权限用户信息异常");
//							resultData.setSuccess(false);
//							resultData.setErrorMsg("获取统一权限用户信息异常");
//						}
//					}else {
//						LOG.error("审批节点approveId不可为空");
//						resultData.setSuccess(false);
//						resultData.setErrorMsg("审批节点approveId不可为空");
//					}
//				}else {
//					LOG.error("业务businessId不可为空");
//					resultData.setSuccess(false);
//					resultData.setErrorMsg("业务businessId不可为空");
//				}
//			}else {
//				LOG.error("必要参数不可为空");
//				resultData.setSuccess(false);
//				resultData.setErrorMsg("必要参数不可为空");
//			}
            User user = this.getLoginUserInfo(request);
            if(user != null){
                resultData=humanManageWorkFlowServerCall.approveWorkFlowDing(flowDto,user.getId(),user.getFax(),user.getName());
            }
		} catch(Exception e) {
			LOG.error("审批业务流程异常", e);
			resultData.setSuccess(false);
			resultData.setErrorMsg("审批业务流程异常");
		}
		return resultData;
	}


	/**
	 * 获取审核进度数据
	 * @author yangwang
	 * @param page 分页
	 * @param process 进度数据
	 * @return ResultData
	 *  */
	@RequestMapping(value = "/approveProcessPage")
	@ResponseBody
	public ResultData approveProcessPage(HttpServletRequest request,Page<ProcessListVo> page,@RequestBody ProcessListVo process) {
		ResultData resultData = new ResultData();
		try {
			User user = this.getLoginUserInfo(request);
			if(user != null){
				process.setDealErpNo(user.getFax());
				resultData=humanManageWorkFlowServerCall.approveProcessPageDing(page.getPageNo(),page.getPageSize(),user.getId(),user.getBaseOrgId(),process);
			}
//			User user = this.getLoginUserInfo(request);
//			if((null != user)&&(StringUtils.isNotBlank(user.getId()))) {
//				if((null != process)&&(StringUtils.isNotBlank(process.getBusinessTypeId()))) {
//					process.setDealErpNo(user.getFax());
//					resultData = workFlowAntService.approveProcessPage(page, process,user);
//					LOG.info("获取审核进度数据成功");
//				}else{
//					LOG.error("参数businessTypeId不可为空");
//					resultData.setSuccess(false);
//					resultData.setErrorMsg("参数businessTypeId不可为空");
//				}
//			}else {
//				LOG.error("获取审核进度数据异常");
//				resultData.setSuccess(false);
//				resultData.setErrorMsg("获取审核进度数据异常");
//			}
		} catch(Exception e) {
			LOG.error("获取审核进度数据异常", e);
			resultData.setSuccess(false);
			resultData.setErrorMsg("获取审核进度数据异常");
		}
		return resultData;
	}

	/**
	 * 根据业务ID获取该业务的审批记录
	 * @author yangwang
	 * @param businessId 业务id
	 * @return ResultData
	 */
	@RequestMapping(value = "/findApprovedRecord")
	public ResultData findApprovedRecord(String businessId) {
		ResultData resultData = new ResultData();
		try {

			resultData=humanManageWorkFlowServerCall.findApprovedRecordDing(businessId);

//			resultData = workFlowAntService.findApprovedRecord(businessId);
			LOG.info("根据业务ID获取该业务的审批记录成功");
		} catch(Exception e) {
			LOG.error("根据业务ID获取该业务的审批记录异常", e);
			resultData.setSuccess(false);
			resultData.setErrorMsg("根据业务ID获取该业务的审批记录异常");
		}
		return resultData;
	}

	/**
	 * 删除业务
	 * @author yangwang
	 * @param businessId 业务id
	 * @return ResultData
	 */
	@RequestMapping(value = "/deleteBusiness")
	public ResultData deleteBusiness(HttpServletRequest request,@RequestBody ProcessListVo process) {
		ResultData resultData = new ResultData();
		try {
			User user = this.getLoginUserInfo(request);
			if(user != null){
				resultData=humanManageWorkFlowServerCall.deleteBusinessDing(process,user.getName(),user.getFax(),user.getId());
			}
//			User user = this.getLoginUserInfo(request);
//			if((null != user)&&(StringUtils.isNotBlank(user.getId()))) {
//				resultData = workFlowAntService.deleteBusiness(user, process);
//				LOG.info("删除业务成功");
//			}else {
//				LOG.error("获取登录用户信息异常");
//				resultData.setSuccess(false);
//				resultData.setErrorMsg("获取登录用户信息异常");
//			}
		} catch(Exception e) {
			LOG.error("删除业务异常", e);
			resultData.setSuccess(false);
			resultData.setErrorMsg("删除业务异常");
		}
		return resultData;
	}


	@RequestMapping(value = "/test")
	@ResponseBody
	public ResultData test(HttpServletRequest request,String sysCode) {
		ResultData resultData = new ResultData();
		try {
			System.out.println("进入测试test。。。。。。");
				resultData = workFlowAntService.test(sysCode);
				System.out.println("结束测试test。。。。。。");
				LOG.info("测试业务成功");
		} catch(Exception e) {
			LOG.error("测试业务异常", e);
			resultData.setSuccess(false);
			resultData.setErrorMsg("测试业务异常");
		}
		return resultData;
	}

}
